package com.plant.tool;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
import javapns.data.Device;
import javapns.data.PayLoad;
public class SendMessage {

	public String IosSendMessage(String[] dt){//-----测试推送消息
		try {
			PayLoad payLoad = new PayLoad();
			payLoad.addAlert("nihao");
			payLoad.addBadge(1);//消息推送标记数，小红圈中显示的数字
			payLoad.addSound("default");//默认响声           
			PushNotificationManager pushManager = PushNotificationManager.getInstance();//注册deviceToken
			for (int i = 0; i < dt.length; i++) {
				pushManager.addDevice("iPhone", dt[i]);
				//				String host= "gateway.sandbox.push.apple.com";
				String host= "gateway.push.apple.com";
				int port = 2195;
				String certificatePath= "/alidata/server/tomcat-7.0.54/webapps/AppPushServiceMrPlants.p12";
				String certificatePassword= "duigu2016";
				try {
					pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
					Device client = pushManager.getDevice("iPhone");
					System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +"");
					pushManager.sendNotification(client, payLoad);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			}
			pushManager.stopConnection();
			pushManager.removeDevice("iPhone");
			System.out.println("执行完毕");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	public String IosSendMessageOne(String deviceToken,String name){//-----好友申请   推送方法
		String mes="推送消息成功";
		try {
			PayLoad payLoad = new PayLoad();
			if(name.length()>4){
				name=name.substring(0,4);
				payLoad.addAlert(name+"向您发起了一个好友申请");
			}else{
				payLoad.addAlert(name+"向您发起了一个好友申请");
			}
			payLoad.addBadge(1);//消息推送标记数，小红圈中显示的数字
			payLoad.addSound("default");//默认响声           
			payLoad.addCustomDictionary("content_type", "addfriend");
			PushNotificationManager pushManager = PushNotificationManager.getInstance();//注册deviceToken
			pushManager.addDevice("iPhone", deviceToken);
			int port = 2195;
			String host= "gateway.push.apple.com";//----发布请求地址
			String certificatePath= "/alidata/server/tomcat-7.0.54/webapps/AppPushServiceMrPlants.p12";//---发布请求证书
			//             String host= "gateway.sandbox.push.apple.com";
			//             String certificatePath= "C:/Users/admin/Desktop/AppPushCer.p12";
			String certificatePassword= "duigu2016";
			pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
			System.out.println("正在执行...");
			Device client = pushManager.getDevice("iPhone");
			System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +"");
			pushManager.sendNotification(client, payLoad);
			pushManager.stopConnection();
			pushManager.removeDevice("iPhone");
			System.out.println("执行完毕");
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return mes;
	}

	public String IosSendMessageFaq(List<String> list,String nickname,Integer faq_id){//-----好友求助推送
		String mes="推送消息成功";
		try {
			PayLoad payLoad = new PayLoad();
			if(nickname.length()>4){
				nickname=nickname.substring(0,4);
				payLoad.addAlert("您的好友"+nickname+"..发出一个求助");
			}else{
				payLoad.addAlert("您的好友"+nickname+"发出一个求助");
			}
			payLoad.addBadge(1);//消息推送标记数，小红圈中显示的数字
			payLoad.addSound("default");//默认响声           
			payLoad.addCustomDictionary("content_type", "faq");
			payLoad.addCustomDictionary("faq_id", faq_id);
			PushNotificationManager pushManager = PushNotificationManager.getInstance();//注册deviceToken
			Set<String> set=new HashSet<String>();
			set=Test.OnlyList(list);
			Iterator<String> it=set.iterator();
			while (it.hasNext()) {
				String divice=it.next();
				if(divice==null||divice==" "){
					System.out.println("为null或为空");
				}else{
					pushManager.addDevice("iPhone",divice);
					String host= "gateway.push.apple.com";//----发布推送网址
					//String host= "gateway.sandbox.push.apple.com";//----开发推送网址
					int port = 2195;
					String certificatePath= "/alidata/server/tomcat-7.0.54/webapps/AppPushServiceMrPlants.p12";
					String certificatePassword= "duigu2016";
					pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
					System.out.println("正在执行...");
					Device client = pushManager.getDevice("iPhone");
					System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +"");
					pushManager.sendNotification(client, payLoad);
					pushManager.stopConnection();
					pushManager.removeDevice("iPhone");
					System.out.println("执行完毕");
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return mes;
	}
	public String IosSendMessageFaqToMyfriend(Set<String> set,String nickname,Integer faq_id){//-----好友求助推送
		String mes="推送消息成功";
		try {
			PayLoad payLoad = new PayLoad();
			if(nickname.length()>4){
				nickname=nickname.substring(0,4);
				payLoad.addAlert("您的好友"+nickname+"..发出一个求助");
			}else{
				payLoad.addAlert("您的好友"+nickname+"发出一个求助");
			}
			payLoad.addBadge(1);//消息推送标记数，小红圈中显示的数字
			payLoad.addSound("default");//默认响声           
			payLoad.addCustomDictionary("content_type", "faq");
			payLoad.addCustomDictionary("faq_id", faq_id);
			PushNotificationManager pushManager = PushNotificationManager.getInstance();//注册deviceToken
			Iterator<String> it=set.iterator();
			while (it.hasNext()) {
				String divice=it.next();
				if(divice==null||divice==" "){
					System.out.println("为null或为空");
				}else{
					pushManager.addDevice("iPhone",divice);
					String host= "gateway.push.apple.com";//----发布推送网址
					//String host= "gateway.sandbox.push.apple.com";//----开发推送网址
					int port = 2195;
					String certificatePath= "/alidata/server/tomcat-7.0.54/webapps/AppPushServiceMrPlants.p12";
					String certificatePassword= "duigu2016";
					pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
					System.out.println("正在执行...");
					Device client = pushManager.getDevice("iPhone");
					System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +"");
					pushManager.sendNotification(client, payLoad);
					pushManager.stopConnection();
					pushManager.removeDevice("iPhone");
					System.out.println("执行完毕");
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return mes;
	}


	public static String Senfaq(Set<String> set,String nickname,Integer faq_id){//-----好友求助推送
		String mes="推送消息成功";
		try {
			PayLoad payLoad = new PayLoad();
			if(nickname.length()>4){
				nickname=nickname.substring(0,4);
				payLoad.addAlert("植物先生"+nickname+"..发出一个求助,需要你的帮助");
			}else{
				payLoad.addAlert("植物先生"+nickname+"发出一个求助,需要你的帮助");
			}
			payLoad.addBadge(1);//消息推送标记数，小红圈中显示的数字
			payLoad.addSound("default");//默认响声           
			payLoad.addCustomDictionary("content_type", "faq");
			payLoad.addCustomDictionary("faq_id", faq_id);
			PushNotificationManager pushManager = PushNotificationManager.getInstance();//注册deviceToken
			if(set.equals(null)){
				System.out.println("token为空");
			}else{
				Iterator<String> it=set.iterator();
				while (it.hasNext()) {
					String divice=it.next();
					if(null==divice||divice==" "){
						System.out.println("为null或为空");
					}else{
						pushManager.addDevice("iPhone",divice);
						String host= "gateway.push.apple.com";//----发布推送网址
						//String host= "gateway.sandbox.push.apple.com";//----开发推送网址
						int port = 2195;
						String certificatePath= "/alidata/server/tomcat-7.0.54/webapps/AppPushServiceMrPlants.p12";
						String certificatePassword= "duigu2016";
						pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
						System.out.println("正在执行...");
						Device client = pushManager.getDevice("iPhone");
						System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +"");
						pushManager.sendNotification(client, payLoad);
						pushManager.stopConnection();
						pushManager.removeDevice("iPhone");
						System.out.println("执行完毕");
					}
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return mes;
	}


	public String IosSendMessageFaqAnswer(String deviceToken,Integer to_faq_id){//------求助回答推送
		String mes="推送消息成功";
		try {
			PayLoad payLoad = new PayLoad();
			payLoad.addAlert("您的求助有一条新的回答");
			payLoad.addBadge(1);//消息推送标记数，小红圈中显示的数字
			payLoad.addSound("default");//默认响声           
			payLoad.addCustomDictionary("content_type", "faqanswer");
			payLoad.addCustomDictionary("to_faq_id", to_faq_id);
			PushNotificationManager pushManager = PushNotificationManager.getInstance();//注册deviceToken
			pushManager.addDevice("iPhone", deviceToken);
			//			String host= "gateway.sandbox.push.apple.com";
			String host= "gateway.push.apple.com";
			int port = 2195;
			String certificatePath= "/alidata/server/tomcat-7.0.54/webapps/AppPushServiceMrPlants.p12";
			String certificatePassword= "duigu2016";
			pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
			System.out.println("正在执行...");
			Device client = pushManager.getDevice("iPhone");
			System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +"");
			pushManager.sendNotification(client, payLoad);
			pushManager.stopConnection();
			pushManager.removeDevice("iPhone");
			System.out.println("执行完毕");
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return mes;
	}


	public String IosSendMessageNewThingComment(String deviceToken,Integer before_release_content_id){//---用户回答新鲜事   消息推送
		String mes="推送消息成功";
		try {
			PayLoad payLoad = new PayLoad();
			payLoad.addAlert("您的好友评论了您的新鲜事");
			payLoad.addBadge(1);//消息推送标记数，小红圈中显示的数字
			payLoad.addSound("default");//默认响声           
			payLoad.addCustomDictionary("content_type", "newthingscomment");
			payLoad.addCustomDictionary("content_id", before_release_content_id);
			PushNotificationManager pushManager = PushNotificationManager.getInstance();//注册deviceToken
			pushManager.addDevice("iPhone", deviceToken);
			//			String host= "gateway.sandbox.push.apple.com";//---测试版推送地址
			String host= "gateway.push.apple.com";
			int port = 2195;
			String certificatePath= "/alidata/server/tomcat-7.0.54/webapps/AppPushServiceMrPlants.p12";
			String certificatePassword= "duigu2016";
			pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
			System.out.println("正在执行...");
			Device client = pushManager.getDevice("iPhone");
			System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +"");
			pushManager.sendNotification(client, payLoad);
			pushManager.stopConnection();
			pushManager.removeDevice("iPhone");
			System.out.println("执行完毕");
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return mes;
	}



	//    public static void main(String[] args) throws Exception {
	//        try {
	//              String deviceToken = "c8f24e72f88e84a9bac0be0d52f9a9696c7d0e9f3fde84611a7691d2c70b7855";
	//              //--定义消息模式
	//              PayLoad payLoad = new PayLoad();
	//              payLoad.addAlert("nihao");
	//              payLoad.addBadge(1);//消息推送标记数，小红圈中显示的数字
	//              payLoad.addSound("default");//默认响声           
	//              PushNotificationManager pushManager = PushNotificationManager.getInstance();//注册deviceToken
	//              pushManager.addDevice("iPhone", deviceToken);
	//              String host= "gateway.sandbox.push.apple.com";
	//              int port = 2195;
	//              String certificatePath= "C:/Users/admin/Desktop/AppPushCer.p12";
	//              String certificatePassword= "duigu2016";
	//              pushManager.initializeConnection(host,port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
	//              System.out.println("正在执行...");
	//              Device client = pushManager.getDevice("iPhone");
	//              System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +"");
	//              pushManager.sendNotification(client, payLoad);
	//              pushManager.stopConnection();
	//              pushManager.removeDevice("iPhone");
	//              System.out.println("执行完毕");
	//        }
	//             catch (Exception e) {
	//              e.printStackTrace();
	//        }
	//    }
}
